function getSelectValues(sel){
	$(hidden).value = sel.value;
	$(input).value  = sel.getElementsByTagName('option')[sel.selectedIndex].innerHTML;
	$(container).removeChild(sel);
}

function delSelectValues(sel){
	$(hidden).value = '';
	$(input).value  = '';
	$(input).focus();
	$(container).removeChild(sel);

}

function ajx(){
	$(imagem).style.display = 'block';
	new ajax(filesearch + $(input).value, {
		update : $(container),
		onComplete : function(){
			setTimeout('setChange();',5);
			$(imagem).style.display = 'none';
		}
	});
}

function setChange(){

	if($(select)){

		$S('#' + select).action({

			initialize : function(){

				$(container).style.position = 'absolute';
				$(container).style.left     = $(input).offsetLeft + 'px';
				$(container).style.top      = $(input).offsetTop + $(input).offsetHeight  + 5 + 'px';

			} ,

			onkeyup : function(e){

				if(!e) e = window.event;
				var key = (e.which || e.keyCode);

				if(key == 13)	{
					getSelectValues(this)	;
				}else if(key == 27)	{
					delSelectValues(this)	;
				}
				
				
    		} ,

			ondblclick : function(){
				getSelectValues(this);
			}

		});

	}
}


window.onload = function(){

	var timeout;
	var oldValue = '';

	$S('#' + input).action({

		onkeyup : function(e){

			if(!e) e = window.event;
			var key = (e.which || e.keyCode);

			if(this.value.length > 2 ){

				if(oldValue != this.value){
					clearTimeout(timeout);
					timeout = setTimeout('ajx()',300);
				}

			}else{

				if($(select)){
					$(container).removeChild($(select));
				}

			}

			oldValue = this.value;

		} ,

		onkeydown : function(e){

			if(!e) e = window.event;
			var key = (e.which || e.keyCode);

			if(key == 40){
				if($(select))
				$(select).focus();
			}

		}

	});

}
